import {
  getLineCodeListNew,
  exportUrlRatio
} from "@/api/report/mesProdOrderPlanPassRatio.js"
import moment from "moment";
import url from "@/axios/url"
const { mesUrl } = url()
let vm = null
const sendThis = (_this) => {
  vm = _this
}
export { sendThis }
// 列表头按钮配置
export const operateList = [
  {
    name: $i18n.t("common.export"),
    type: "primary",
    icon: "el-icon-download",
    handleName: "export", // 点击时返回的标识
    url: exportUrlRatio
    // auth: "auth:operlog:export", //权限字段
  }
]
// 行末按钮配置
export const rowConfig = [
  // {
  //   name: "修改",
  //   type: "primary",
  //   class: "row-btn-primary",
  //   handleName: "edit", // 点击时返回的标识
  // },
  // {
  //   name: "详情",
  //   type: "info",
  //   class: "row-btn-info",
  //   handleName: "detail", // 点击时返回的标识
  // },
  // {
  //   name: "删除",
  //   type: "danger",
  //   class: "row-btn-danger",
  //   handleName: "delete", // 点击时返回的标识
  // },
]
// 搜索条件配置
export const searchConfig = {
  data () {
    const self = this
    var choiceDate = null
    return {
      searchConfig: {
        inline: true,
        formDesc: {
          searchTimeType: {
            type: "radio-button",
            layout: 5,
            labelWidth: '10px',
            options: [{ text: $i18n.t("common.yesterday"), value: "0" }, { text: $i18n.t("common.today"), value: "1" }, { text: $i18n.t("common.thisWeek"), value: "2" }, { text: $i18n.t("mesProductionDailyClear.thisMonth"), value: "3" }],
            on: {
              change: (val) => {
                self.timeChange(val)
              }
            }
          },
          actualEndDate: {
            type: "daterange",
            layout: 7,
            label: $i18n.t("mesProdOrderPlanPassRatio.actualEndDate"),
            attrs: {
              valueFormat: "yyyy-MM-dd",
              searchArrKey: ["actualEndDateMin", "actualEndDateMax"],
              clearable: false,
              pickerOptions: {
                onPick: ({ maxDate, minDate }) => {
                  // 把选择的第一个日期赋值给一个变量。
                  choiceDate = minDate.getTime()

                  // 如何你选择了两个日期了，就把那个变量置空
                  if (maxDate) choiceDate = ''
                },
                disabledDate: (time) => {
                  // 如何选择了一个日期
                  if (choiceDate) {
                    // 7天的时间戳
                    const one = 30 * 24 * 3600 * 1000
                    // 当前日期 - one = 7天之前
                    const minTime = choiceDate - one
                    // 当前日期 + one = 7天之后
                    const maxTime = choiceDate + one
                    return time.getTime() < minTime || time.getTime() > maxTime
                  } else {
                    // 如果没有选择日期，就要限制不能选择今天及以后
                    // return time.getTime() > Date.now();
                  }
                }
              }
            }
          },
          workshopCode: {
            label: $i18n.t("mesProdOrderPlanPassRatio.workshopName"),
            type: "select",

            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            options: async (data) => {
              const params = {
                modelingCode: self.$store.getters.factoryCode,
                level: 4,
                modelingType: "0",
              };
              const list = await getLineCodeListNew(params);
              return list.data.result;
            },
          },
          lineCode: {
            label: $i18n.t("mesProdOrderPlanPassRatio.lineName"),
            type: "select",
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            optionsLinkageFields: ["workshopCode"],
            options: async (data) => {
              if (data && data.workshopCode) {
                const params = {
                  modelingCode: data.workshopCode,
                  level: 5,
                };
                const list = await getLineCodeListNew(params);
                return list.data.result;
              } else {
                return []
              }
            },
          },
          planCode: {
            type: "input",
            label: $i18n.t("mesProdOrderPlanPassRatio.planCode")
          },
        }
      }
    }
  },
  methods: {
    timeChange(val) {
      let data =   this.$refs.eleTable.getSearchParams()
      if (val == '0') {
        let timer = moment().subtract(1, "days").format("YYYY-MM-DD");
        this.initForm = {...data, searchTimeType: val, actualEndDate: [timer, timer] }
        console.log("initForm", this.initForm)
      } else if (val == '1') {
        this.initForm = {...data, searchTimeType: val, actualEndDate: [moment().format("YYYY-MM-DD"), moment().format("YYYY-MM-DD")] }
      } else if (val == '2') {
        let startWeek = moment().startOf("isoWeek").format("YYYY-MM-DD");
        let endWeek = moment().endOf("isoWeek").format("YYYY-MM-DD");
        this.initForm = {...data, searchTimeType: val, actualEndDate: [startWeek, endWeek] }
      } else {
        let startMonth = moment().startOf("month").format("YYYY-MM-DD");
        let endMonth = moment().endOf("month").format("YYYY-MM-DD");
        this.initForm = {...data, searchTimeType: val, actualEndDate: [startMonth, endMonth] }
      }
    }
  }
}

export const configformDescDialog = {
  data () {
    return {
      // 新增/删除表单配置
      formDescDialog: {
        span: 24,
        disabled: false,
        isShowSubmitBtn: true,
        formDesc: {
          // factoryCode: {
          //   type: "select",
          //   label: "工厂编码",
          //   layout: 12,
          //   required: true,
          //   prop: {
          //     value: "modelingCode",
          //     text: "modelingName",
          //   },
          //   options: async (data) => {
          //     const list = await getFactoryCodeList()
          //     return list.data.result
          //   },
          // },
          // saleCode: {
          //   type: "select",
          //   label: "销售订单",
          //   layout: 8,
          //   required: true,
          // },
          orderCode: {
            type: "input",
            label: $i18n.t("mesProdOrderPlanPassRatio.orderCode"),
            layout: 8,
            disabled: (d) => !!d.id,
            required: true
          },
          prodCode: {
            type: "input",
            label: $i18n.t("mesProdOrderPlanPassRatio.prodCode"),
            layout: 8,
            required: true
          },

          prodName: {
            type: "input",
            label: $i18n.t("mesProdOrderPlanPassRatio.prodName"),
            layout: 8,
            required: false,
            disabled: true
          },
          seriesCode: {
            type: "input",
            label: $i18n.t("mesProdOrderPlanPassRatio.seriesCode"),
            layout: 8,
            required: true,
            disabled: true
          },

          seriesName: {
            type: "input",
            label: $i18n.t("mesProdOrderPlanPassRatio.seriesName"),
            layout: 8,
            disabled: true
          },
          measureUnits: {
            type: "input",
            label: $i18n.t("mesProdOrderPlanPassRatio.measureUnits"),
            layout: 8,
            disabled: true
          },
          bomVersionNumber: {
            type: "input",
            label: $i18n.t("mesProdOrderPlanPassRatio.bomVersionNumber"),
            layout: 8,
            required: true
          },
          simulatedType: {
            type: "select",
            label: $i18n.t("mesProdOrderPlanPassRatio.simulatedType"),
            layout: 8,
            required: true,
            prop: {
              value: "value",
              text: "description"
            },
            options: async (data) => {
              const list = await getSimulatedTypeList()
              return list.data.result
            }
          },
          prodOrderType: {
            type: "select",
            label: $i18n.t("mesProdOrderPlanPassRatio.prodOrderType"),
            layout: 8,
            required: true,
            prop: {
              value: "value",
              text: "description"
            },
            options: async (data) => {
              const list = await getProdOrderTypeList()
              return list.data.result
            }
          },
          // priority: {
          //   type: "number",
          //   label: "订单优先级",
          //   layout: 8,
          //   required: true,
          //   min: 1,
          // },
          // technologyId: {
          //   type: "input",
          //   label: "工艺建模流程主键",
          //   layout: 12,
          //   required: true,
          // },
          deliveryDate: {
            type: "date",
            label: $i18n.t("mesProdOrderPlanPassRatio.deliveryDate"),
            layout: 8,
            required: false,
            attrs: {
              valueFormat: "yyyy-MM-dd"
            }
          },
          slackTime: {
            type: "number",
            label: $i18n.t("mesProdOrderPlanPassRatio.slackTime"),
            layout: 8,
            required: false
          },
          quantity: {
            type: "number",
            label: $i18n.t("mesProdOrderPlanPassRatio.quantity"),
            layout: 8,
            required: true
          },

          planBeginDate: {
            type: "date",
            label: $i18n.t("mesProdOrderPlanPassRatio.planBeginDate"),
            layout: 8,
            required: true,
            attrs: {
              valueFormat: "yyyy-MM-dd"
            }
          },
          planEndDate: {
            type: "date",
            label: $i18n.t("mesProdOrderPlanPassRatio.planEndDate"),
            layout: 8,
            required: false,
            attrs: {
              valueFormat: "yyyy-MM-dd"
            }
          },
          advanceCode: {
            type: "input",
            label: $i18n.t("mesProdOrderPlanPassRatio.advanceCode"),
            layout: 8,
            required: false
          },
          backflushType: {
            type: "select",
            label: $i18n.t("mesProdOrderPlanPassRatio.backflushType"),
            layout: 8,
            required: false,
            prop: {
              value: "value",
              text: "description"
            },
            options: async (data) => {
              const list = await getBackflushTypeList()
              return list.data.result
            }
          },
          remark: {
            type: "textarea",
            label: $i18n.t("mesProdOrderPlanPassRatio.remark"),
            layout: 8,
            required: false
          }
        }
      }
    }
  }
}

export const configformDescDialog_generate = {
  data () {
    const _this = this
    return {
      // 下发表单配置
      formDescDialog_generate: {
        span: 24,
        disabled: false,
        isShowSubmitBtn: true,
        ids: "",
        submitProdLineList: "",
        formDesc: {
          lineCode: {
            type: "select",
            label: $i18n.t("mesProdOrderPlanPassRatio.lineCode"),
            layout: 24,
            required: true,
            attrs: {
              filterable: true
            },
            prop: {
              value: "modelingCode",
              text: "modelingName"
            },
            options: async (data) => {
              const params = {
                modelingCode: this.$store.getters.factoryCode,
                level: 5
              }
              const list = await getLineCodeList(params)
              this.lineList = list.data.result
              return list.data.result
            }
          },
          orderBarcodeRuleCode: {
            type: "select",
            label: $i18n.t("mesProdOrderPlanPassRatio.orderBarcodeRuleCode"),
            layout: 24,
            required: true,
            prop: {
              value: "code",
              text: "description"
            },
            options: async (data) => {
              const list = await getOrderBarcodeRuleList()
              this.orderBarcodeRuleList = list.data.result
              return list.data.result
            }
          }
        }
      }
    }
  }
}

